package com.test.springboot.springBootUtils.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Path;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;


import com.test.springboot.springBootUtils.bean.AbsTask;

/**
 * 手动校验JAVABean的报文字段是否符合格式
 * @author huyong
 *
 */
public class HeadValidJavaBean {
	private static ValidatorFactory factory = Validation.buildDefaultValidatorFactory();

	public void testValidJavaBean() {
		AbsTask absTask = new AbsTask();
		absTask.setTaskId("44444444444444444444444");
		absTask.setSourceName("esop");
		absTask.setOfferNum("50025");
		absTask.setPoOrdPackageNum("510A898989");
		absTask.setTaskRole("退回修改");
		absTask.setCustomerNumber("010010");
		absTask.setPackageOperSubType("4");
		absTask.setFlag(true);
		System.out.println(validate(absTask));
	}

	public static <T> List<String> validate(T t) {
		Validator validator = factory.getValidator();
		Set<ConstraintViolation<T>> validate = validator.validate(t);
		ArrayList<String> messageList = new ArrayList<String>();
		for (ConstraintViolation<T> constraintViolation : validate) {
			Path propertyPath = constraintViolation.getPropertyPath();
			System.out.println(propertyPath);
			messageList.add(constraintViolation.getMessage());
		}
		return messageList;
	}
}
